package com.ly.cloud.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.ly.cloud.common.mybatisplus.plugins.PageInfo;
import com.ly.cloud.dto.CourseTempDetailDto;
import com.ly.cloud.dto.CourseTempDto;
import com.ly.cloud.service.CourseTimeService;
import com.ly.cloud.vo.CourseTempCasVo;
import com.ly.cloud.vo.CourseTempDetailVo;
import com.ly.cloud.vo.CourseTempVo;
import com.ly.cloud.web.utils.WebResponse;

/**
 * 课程学时模板Controller
 * @author caihuihai
 * @date 2018年12月17日 下午4:30:11
 */
@RestController
@RequestMapping("/courseTime")
public class CourseTimeController {
	
	@Autowired
    private CourseTimeService courseTimeService ;
	
	/**
     * (测试)根据表编号查询编码规则，并返回按规则生成的对应值
     * @param bbh 表编号
     * @return
     */
    @RequestMapping(value = "/test/{bbh}", method = RequestMethod.GET)
    public WebResponse<String> genRuleCode(@RequestHeader(required = false, value = "loginUserId") String loginUserId,
    		@RequestHeader(required = false, value = "loginUserName") String loginUserName,
    		@PathVariable String bbh){
        String code = courseTimeService.testGen(loginUserId,loginUserName,bbh);
        return new WebResponse<String>().success(code);
    }
    
    
    /**
	 * 分页查询课程学时模板列表
	 * @return
	 */
	@RequestMapping(value = "/queryList", method = RequestMethod.GET)
	public WebResponse<PageInfo<CourseTempVo>> queryPageList(@RequestParam int pageNum, @RequestParam int pageSize) {
		Page<CourseTempVo> page = courseTimeService.selectPage(new Page<CourseTempVo>(pageNum, pageSize));
		return new WebResponse<PageInfo<CourseTempVo>>().success(PageInfo.toPageInfo(page));
	}
	
	/**
	 * 提交课程学时模板（新增、修改、启用/停用、设置默认/取消默认）
	 * @param dto
	 * @return
	 */
	@RequestMapping(value = "/submitMb", method = RequestMethod.POST)
	public WebResponse<Boolean> submitMb(@RequestHeader(required=false,value="loginUserName")String loginUserName,@RequestBody CourseTempDto dto) {
		boolean rs = courseTimeService.submitMb(dto,loginUserName);
		return new WebResponse<Boolean>().success(rs);
	}
	
	/**
	 * 根据课程模板编号查询课程模板信息
	 * @param kcmbbh
	 * @return
	 */
	@RequestMapping(value = "/findMbByBh", method = RequestMethod.GET)
	public WebResponse<CourseTempVo> findMbByBh(@RequestParam("kcmbbh") Long kcmbbh) {
		CourseTempVo vo = courseTimeService.findMbByBh(kcmbbh);
		return new WebResponse<CourseTempVo>().success(vo);
	}
	
	/**
	 * 删除课程模板信息,并把对应的模板学时信息也删除
	 * @param kcmbbh
	 * @return
	 */
	@RequestMapping(value = "/delMbByBh/{kcmbbh}", method = RequestMethod.POST)
	public WebResponse<Boolean> delMbByBh(@PathVariable("kcmbbh") Long kcmbbh) {
		boolean rs = courseTimeService.delMbByBh(kcmbbh);
		return new WebResponse<Boolean>().success(rs);
	}
	
	
	/**
	 * 根据课程编号查询课程学时列表
	 * @param kcmbbh
	 * @return
	 */
	@RequestMapping(value = "/queryKcxsList", method = RequestMethod.GET)
	public WebResponse<List<CourseTempDetailVo>> queryKcxsList(@RequestParam("kcmbbh") Long kcmbbh) {
		List<CourseTempDetailVo> list = courseTimeService.queryKcxsList(kcmbbh);
		return new WebResponse<List<CourseTempDetailVo>>().success(list);
	}
	
	
	/**
	 * 点击新增学时按钮时的查询代码集list，需要过滤已选择的学时
	 * @param kcmbbh
	 * @param xsbh
	 * @param xsmc
	 * @return
	 */
	@RequestMapping(value = "/queryKcxsDmj", method = RequestMethod.GET)
	public WebResponse<PageInfo<CourseTempDetailVo>> queryKcxsDmj(@RequestParam int pageNum, @RequestParam int pageSize,@RequestParam("kcmbbh") Long kcmbbh,
										@RequestParam(required=false,value="xsbh") String xsbh,@RequestParam(required=false,value="xsmc") String xsmc) {
		Page<CourseTempDetailVo> page = courseTimeService.queryKcxsDmjPage(new Page<CourseTempDetailVo>(pageNum, pageSize),kcmbbh,xsbh,xsmc);
		return new WebResponse<PageInfo<CourseTempDetailVo>>().success(PageInfo.toPageInfo(page));
	}
	
	
	/**
	 * 保存新增的学时信息
	 * @param dto
	 * @return
	 */
	@RequestMapping(value = "/insertMbxs", method = RequestMethod.POST)
	public WebResponse<Boolean> insertMbxs(@RequestBody List<CourseTempDetailDto> list) {
		boolean rs = courseTimeService.insertMbxs(list);
		return new WebResponse<Boolean>().success(rs);
	}
	
	/**
	 * 删除学时信息
	 * @param kcmbbh  
	 * @param xsbh
	 * @return
	 */
	@RequestMapping(value = "/deleteKcxs/{kcmbbh}/{xsbh}", method = RequestMethod.POST)
	public WebResponse<Boolean> deleteKcxs(@PathVariable("kcmbbh") Long kcmbbh,@PathVariable("xsbh") String xsbh) {
		boolean rs = courseTimeService.deleteKcxs(kcmbbh,xsbh);
		return new WebResponse<Boolean>().success(rs);
	}
	
	
	/**
	 * 查询课程学时级联列表（通用）
	 * @return
	 */
	@RequestMapping(value = "/findKcxsCasList", method = RequestMethod.GET)
	public WebResponse<List<CourseTempCasVo>> findKcxsCasList() {
		List<CourseTempCasVo> list = courseTimeService.findKcxsCasList();
		return new WebResponse<List<CourseTempCasVo>>().success(list);
	}
	

}
